package com.cdtu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cdtu.entity.Admin;
import com.cdtu.entity.BelongCommunity;
import com.cdtu.entity.Notice;
import com.cdtu.service.AdminService;
import com.cdtu.service.BelongCommunityService;
import com.cdtu.service.NoticeService;
import com.cdtu.vo.R;
import com.cdtu.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RequestMapping("/notice")
@RestController
public class NoticeController {
    @Value("${reggie.path}")
    private String basePath;
    @Autowired
    private NoticeService noticeService;
    @Autowired
    AdminService adminService;
    @Autowired
    BelongCommunityService belongCommunityService;
    @Autowired
    private StudyFileController studyFileController;

    //    查询所有的公告分页展示
    @GetMapping("/queryAll")
    public Map<String, Object> queryAll(Integer pageCurrent, Integer pageSize, String value) {
//        System.out.println("我要看的数据  pageCurrent="+pageCurrent+"  pageSize="+pageSize+"  value="+value);
        Map<String, Object> result = new HashMap<>();
        pageCurrent = pageCurrent == null ? 1 : pageCurrent;
        pageSize = pageSize == null ? 8 : pageSize;
        List<Notice> notice = noticeService.selectByPage(pageCurrent, pageSize,value);
        long total;
        if ("".equals(value)) total = noticeService.findTotal();
        else total = noticeService.findByNameTotal(value);
        result.put("msg",true);
        result.put("noticeData",notice);
        result.put("total",total);

        return result;
    }
    @GetMapping("/queryUnRollingAll")
    public Map<String, Object> queryUnRollingAll(Integer pageCurrent, Integer pageSize, String value) {
        Map<String, Object> result = new HashMap<>();
        pageCurrent = pageCurrent == null ? 1 : pageCurrent;
        pageSize = pageSize == null ? 5 : pageSize;
        int priority=0;
        List<Notice> notice = noticeService.selectRollingByPage(pageCurrent, pageSize,value,priority);
        long total;
        if ("".equals(value)) total = noticeService.findUnRollingTotal();
        else total = noticeService.findUnRollingByNameTotal(value);
        result.put("msg",true);
        result.put("noticeData",notice);
        result.put("total",total);

        return result;
    }


    //    查询正在进行滚动展示的公告数量
    @GetMapping("/queryRollingLength")
    public Map<String, Object> queryRollingLength() {
        Map<String, Object> result = new HashMap<>();
        int length=noticeService.queryRollingLength();

        result.put("msg",true);
        result.put("length",length);

        return result;
    }

    //    添加公告进行滚动展示
    @PostMapping("/addRolling")
    public Result addRolling(Long id) {
        Result result = new Result();
        boolean res=noticeService.updateById(id,1);
        if (res){
            result.setStatus(true);
        }else result.setStatus(false);
        return result;
    }

    //删除公告
    @GetMapping("/delete")
    public Result delete(@RequestParam(required = false ,name = "id")Long id) {
        Result result = new Result();
        boolean res=noticeService.deleteByID(id);
        Notice notice = noticeService.selectById(id);
        String imageSrc = notice.getImageSrc().substring(notice.getImageSrc().indexOf("=") + 1);
        Boolean s=studyFileController.uploadCustomPathFile(basePath+imageSrc);
        if (res){
            if (s){
                result.setMsg("删除成功！");
                result.setStatus(true);
            }else{
                result.setMsg("图片删除失败！(公告已删除)");
                result.setStatus(false);
            }
        }else {
            result.setMsg("公告删除失败");
            result.setStatus(false);
        }
        return result;
    }

    //    新增公告
    @PostMapping("/addAnnouncement")
    public Result addAnnouncement(@RequestBody Notice notice) {
        Result result = new Result();
        Date date=new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

        notice.setPriority(0);
        notice.setId(Long.valueOf(format.format(date)));
        notice.setPublishTime(date);
        notice.setUpdateTime(date);
        notice.setPlace(null);
        boolean res = noticeService.insertNewNotice(notice);
        if (res){
            result.setStatus(true);
        }else result.setStatus(false);
        return result;
    }

    //    获取需要进行编辑的公告的信息
    @GetMapping("getEdit")
    public Map<String, Object> getEdit(Long id) {
        Map<String, Object> result = new HashMap<>();

        Notice notice=noticeService.selectById(id);
        result.put("title",notice.getTitle());
        result.put("content",notice.getContent());
        result.put("digest",notice.getDigest());
        result.put("imageSrc",notice.getImageSrc());
        result.put("msg",true);

        return result;
    }

    //    修改公告信息
    @PostMapping("/editAnnouncement")
    public Result editAnnouncement(@RequestBody Notice notice) {
        Date date=new Date();

        Result result = new Result();
        notice.setUpdateTime(date);
        boolean res=noticeService.updateByCondition(notice);
        if (res){
            result.setStatus(true);
        }else result.setStatus(false);
        return result;
    }

    //获取正在滚动展示的公告
    @GetMapping("queryRolling")
    public List<Notice> queryRolling() {
        return noticeService.selectByPriority();
    }

    //    取消正在滚动展示的公告
    @PostMapping("/cancelRolling")
    public Result cancelRolling(Long id){
        Result result = new Result();
        boolean res=noticeService.updateById(id,0);
        if (res){
            result.setStatus(true);
        }else result.setStatus(false);
        return result;
    }

    /**
    * 查询所以notice
    * @author wp
    * @param
    * @date 2023/3/3 19:19
    * @return
    */
    @GetMapping("/allNotice/{len}")
    public R getAllNotice(@PathVariable("len") int len){
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Notice::getPublishTime);
        List<Notice> list = noticeService.list(queryWrapper);
        if (list.size()<len+10){
            return  R.success( list.subList(len,list.size()));
        } else {
            return  R.success( list.subList(len,len+10));
        }
    }
    /**
    * 根据公告id回去公告的详细详细
    * @author wp
    * @param
    * @date 2023/3/3 20:33
    * @return
    */
    @GetMapping("/getNotice")
    public R getByIdNotice(long id){
        Notice notice = noticeService.getById(id);
        if (notice!=null)
        return R.success(notice);
        return R.error("服务器错误");
    }
    /**
    * 根据管理员的id查询管理员的名称
    * @author wp
    * @param
    * @date 2023/4/21 16:32
    * @return
    */
    @GetMapping("/auth")
    public  R  getAdminName(@RequestParam("id") long id){
        HashMap<String ,String> map = new HashMap<>();
        Admin admin = adminService.getById(id);
        if (admin != null){
            map.put("authName",admin.getName());
            BelongCommunity c = belongCommunityService.getById(admin.getCommunityId());
            if (c != null){
                map.put("community",c.getCommunity());
            }
        }        return R.success(map);
    }
}
